package com.guxingke.gblog.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.guxingke.gblog.dmo.Msg;
import com.guxingke.gblog.dmo.User;
import com.guxingke.gblog.dto.MsgDto;
import com.guxingke.gblog.dto.UserDto;
import com.guxingke.gblog.service.MsgService;
import com.guxingke.gblog.service.UserService;
import com.guxingke.gblog.service.impl.MsgServiceImpl;
import com.guxingke.gblog.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

public class MsgAction extends ActionSupport implements ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpSession session;
	private MsgService msgService = new MsgServiceImpl();
	private UserService userService = new UserServiceImpl();

	private List<MsgDto> msgs;
	private int type;
	private int replytype;
	private int pid;
	private int articleId;
	private String mainPage;
	private String error;

	private Msg msg;
	private User user;

	@Override
	public String execute() throws Exception {
		System.out.println("execute method");
		return SUCCESS;
	}

	public String leaveMsg() throws Exception {
		type = 1;
		replytype = 3;
		msgs = msgService.getLeaveMsgs();
		mainPage = "template/leavemsg/detail.jsp";
		return SUCCESS;
	}

	public String save() throws Exception {
		if ("".equals(user.getNickname()) || "".equals(msg.getContent())
				|| "".equals(user.getEmail())) {
			request.setAttribute("error", "昵称、邮箱和内容不可为空");
			return ERROR;
		}
		UserDto currentUser = (UserDto) session.getAttribute("currentUser");
		if (currentUser != null) {
			int userId = currentUser.getId();
			msg.setUserId(userId);
			msgService.saveMsg(msg);
		} else {
			user.setUsername(user.getNickname());
			user.setPassword(user.getEmail());
			UserDto cUser = userService.login(user);
			if (cUser == null) {
				user.setImageName("default.jpg");
				userService.saveUser(user);
				cUser = userService.login(user);
			}
			msg.setUserId(cUser.getId());
			msgService.saveMsg(msg);
		}
		if (msg.getType() % 2 != 0) {
			return "leaveMsg";
		} else {
			return "article";
		}
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public List<MsgDto> getMsgs() {
		return msgs;
	}

	public void setMsgs(List<MsgDto> leaveMsgs) {
		this.msgs = leaveMsgs;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getReplytype() {
		return replytype;
	}

	public void setReplytype(int replytype) {
		this.replytype = replytype;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public int getArticleId() {
		return articleId;
	}

	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.session = request.getSession();
	}

	public Msg getMsg() {
		return msg;
	}

	public void setMsg(Msg msg) {
		this.msg = msg;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
